package com.ia.aistream.manager.operating.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class SerialNumberParser {

    public static void main(String[] args) {

        System.out.println(extractSerialNo("122LP"));
        System.out.println(extractSerialNo("121SPd"));
        System.out.println(extractSerialNo("121LG"));
        System.out.println(extractSerialNo("1 ATE 1281 VL"));
        System.out.println(extractSerialNo("1ATE461/462/463ZV"));
        System.out.println(extractSerialNo("1 ATE 201-204LG"));
        System.out.println(extractSerialNo("1ATE/462/463ZV"));
        System.out.println(extractSerialNo("ATE 1281 VL"));
        System.out.println(extractSerialNo("1ATE461/462/463ZV"));
    }
    public static List<String> extractSerialNo(String serialNo) {
        if (serialNo == null || serialNo.isEmpty()) {
            List<String> result = new ArrayList<>();
            result.add(serialNo);
            return result;
        }

        char[] chars = serialNo.toCharArray();
        int variableSegmentEnd = -1;
        for (int i = chars.length - 1; i >= 0; i--) {
            if (Character.isDigit(chars[i])) {
                variableSegmentEnd = i;
                break;
            }
        }

        if (variableSegmentEnd <= 0) {
            List<String> result = new ArrayList<>();
            result.add(serialNo);
            return result;
        }

        int variableSegmentStart = -1;
        boolean variable = false;
        for (int i = variableSegmentEnd; i >= 0; i--) {
            char c = chars[i];
            boolean isVariableFlagChar = (c == '-' || c == '/');
            variable = variable || isVariableFlagChar;
            if (!Character.isDigit(c) && !isVariableFlagChar) {
                variableSegmentStart = i;
                break;
            }
        }

        if (!variable) {
            List<String> result = new ArrayList<>();
            result.add(serialNo);
            return result;
        }
        return extract(serialNo, variableSegmentStart, variableSegmentEnd);
    }

    private static List<String> extract(String serialNo, int variableSegmentStart, int variableSegmentEnd) {
        String prefix = serialNo.substring(0, variableSegmentStart + 1);
        int endIdx = Math.min(variableSegmentEnd + 1, serialNo.length());
        String suffix = serialNo.substring(endIdx);
        String variableSegment = serialNo.substring(variableSegmentStart + 1, endIdx);
        String[] segments = variableSegment.split("/");
        List<String> result = new ArrayList<>();
        for (String segment : segments) {
            if (segment == null || segment.length() == 0) {
                continue;
            }
            String[] numberSegments = segment.split("-");
            if (numberSegments.length == 1) {
                result.add(prefix + numberSegments[0] + suffix);
                continue;
            }
            List<Integer> nums = Arrays.stream(numberSegments).map(SerialNumberParser::toInt).sorted().collect(Collectors.toList());
            for (int i = nums.get(0); i <= nums.get(nums.size() - 1); i++) {
                result.add(prefix + i + suffix);
            }
        }
        return result;
    }

    private static int toInt(String str) {
        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
            return 0;
        }
    }
}
